import java.util.Stack;

/**
* @description: 946. 验证栈序列
* @author hewei
* @date 2022/8/31 14:13
* @version 1.0
*/

public class ValidateStackSequences {

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int p1 = 0;
        int p2 = 0;
        while (p2 < popped.length) {
            if (stack.isEmpty() || stack.peek() != popped[p2]) {
                if (p1 == pushed.length) return false;
                stack.push(pushed[p1++]);
            } else {
                stack.pop();
                ++p2;
            }
        }
        return true;
    }
}
